const qcloud = require('../../vendor/qcloud-weapp-client-sdk/index.js')
const config = require('../../config.js')
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    paramData:{},
    department:0,
    totalExcursion:0,
    victory:0,
    draw:0,
    rUId:'',
    name: "",
    averageWins:0,
    shareState:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUid(options)
    this.setData({
      ownInfo: app.globalData.ownInfo
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   
  onReady: function (options) {
    this.getUid(options)
    
  },*/

  // 页面离开
  onUnload: function () {

    wx.closeSocket((res) => {
      console.log('WebSocket 已关闭！')
    })
  },

  getUid(options) {
    
    if (!!options && JSON.stringify(options) != '{}') {
      this.setData({
        'shareState': true
      })

      this.setData({
        'uId': options.uId || app.globalData.uId
      })
      
    } else {
      this.setData({
        'uId': app.globalData.uId || 38
      })
    }

    this.obtainResultLast(this.data.uId)
  },

  // 返回计算率
  setcalculate(param) {
    this.setData({
      department: Math.floor(param.departmentRanking / param.departmentUserCount * 100 ) 
    })

    this.setData({
      totalExcursion: Math.floor(param.bankRanking / param.bankUserCount * 100)
    })

    this.setData({
      victory: Math.floor(param.winCount / param.matchCount * 100)
    })

    this.setData({
      draw: Math.floor(param.tieCount / param.matchCount * 100)
    })

    this.setData({
      averageWins: Math.floor(param.winCount / param.matchCount * 100)
    })

    this.setData({
      avatarUrl: param.avatarUrl
    })

    this.setData({
      name: param.name
    })

    this.setData({
      uId: app.globalData.uId || param.uId
    })
  },

  obtainResultLast(uId) {
    qcloud.request({
      url: config.service.obtainMatchPersonRanking,
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      login: true,
      data: {
        'rUId': uId
      },
      success: (response) => {
        let data = response.data
        if(data.code == 0 ) {
          this.setData({
            paramData: data.matchUserStatDTO
          })
          this.setcalculate(this.data.paramData)
        } else {
          console.log(data.message)
        }
      },
      fail: function (err) {
        console.log(err);
      }
    })
  },

  // 分享

  onShareAppMessage: function () {
    return {
      title: '来看看我的成绩~',
      desc: '来看看我的成绩~',
      path: '/pages/officialRanking/officialRanking?uId=' + this.data.uId
    }
  }
})